package com.yycx.module.bus.provider.handler.haiying.deptpay;

import com.yycx.bpm.provider.handler.TaskCompleteHandler;
import com.yycx.bpm.provider.model.TaskRequest;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.security.OpenUser;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.BusInvoiceunit;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.entity.BusSalemanBank;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.BusInvoiceunitService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import com.yycx.module.bus.provider.service.BusSalemanBankService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.bus.provider.service.hy.HyOrderDetailService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 获取任务表单数据
 *
 * @author zyf
 */
@Component
public class DeptPayFormTaskDataHandler implements TaskCompleteHandler {

    @Resource
    private BusPaymentService paymentService;

    @Resource
    private HyOrderDetailService orderDetailService;

    @Resource
    private BusInvoiceunitService invoiceunitService;


    @Override
    public Map<String, Object> initTaskData(ProcessInstance processInstance, Task task, Map<String, Object> variables) {
        Map<String, Object> ret = new HashMap<>(5);
        String businessKey = processInstance.getBusinessKey();
        BusPayment payment = paymentService.getById(businessKey);
        if (FlymeUtils.isNotEmpty(payment)) {
            String amountType = payment.getAmountType();
            String bankName = "";
            String bankNo = "";
            String userName = "";
            //部门结算
            if (AmountTypeEnum.Invoice.getCode().equals(amountType)) {
                Long invoiceId = payment.getInvoiceUnitId();
                BusInvoiceunit invoiceunit = invoiceunitService.getById(invoiceId);
                bankName = invoiceunit.getBankName();
                bankNo = invoiceunit.getBankNo();
                userName = invoiceunit.getInvoiceunitName();
            }

            ret.put("bankName", bankName);
            ret.put("bankNo", bankNo);
            ret.put("paymentId", payment.getPaymentId());
            ret.put("userName", userName);
            ret.put("payAmount", payment.getInvoiceAmount());

        }
        return ret;
    }

    /**
     * 表单提交后业务处理
     *
     * @param taskRequest
     * @param processInstance
     * @param task
     * @param completeVariables
     */
    @Override
    public void completeAfter(TaskRequest taskRequest, ProcessInstance processInstance, Task task, Map<String, Object> completeVariables, OpenUser user) {
        String businessKey = processInstance.getBusinessKey();
        BusPayment payment = paymentService.getById(businessKey);
        String amountType = payment.getAmountType();
        Object payBankId = completeVariables.get("payBankId");
        Object payDate = completeVariables.get("payDate");
        if (FlymeUtils.isNotEmpty(payment)) {
            payment.setPayBankId(Long.parseLong(payBankId.toString()));
            payment.setPayDate(DateUtils.strToDate(payDate.toString()));
            payment.setPayUserId(user.getUserId());
            payment.setPaymentState(CommonConstants.INT_2);
            //部门结算
            if (AmountTypeEnum.Invoice.getCode().equals(amountType)) {
                payment.setPaymentState(CommonConstants.INT_2);
                //更新部门结算信息
                orderDetailService.updateDeptSettleAmount(payment);
            }
            paymentService.updateById(payment);
        }
    }

}
